import axios from "axios";
import { useRoute } from "vue-router";
// https://auth.sinopharm.com/idp/oauth2/authorize?client_id=gyzwt&redirect_uri=http://10.184.169.154&response_type=code&ssoType=wps&state=e037d8f7a88f4b18ac94
export default function zhuYunHook() {
  const route = useRoute();
  const zhuYunFun = async () => {
    if (route.query.code) {
      try {
        //  2. 统一认证获取token
        let response: any = await axios({
          url: `/idp/oauth2/getToken`,
          method: "post",
          params: {
            client_id: import.meta.env.VITE_APP_CLIENT_ID,
            client_secret: import.meta.env.VITE_APP_CLIENT_SECRET,
            code: route.query.code,
            grant_type: "authorization_code",
          },
        });
        // 3. 统一认证 获取用户信息
        let pm = {
          client_id: import.meta.env.VITE_APP_CLIENT_ID,
          access_token: response.data.access_token,
        };
        let userInfo: any = await axios({
          url: `/idp/oauth2/getUserInfo`,
          method: "get",
          params: pm,
        });
        localStorage.setItem("user_name", userInfo.data.loginName);
        return true;
      } catch (error) {
        console.log(error);
        return false;
      }
    } else {
      window.location.href =
        import.meta.env.VITE_APP_AUTH_URL +
        "?client_id=" +
        import.meta.env.VITE_APP_CLIENT_ID +
        "&redirect_uri=" +
        import.meta.env.VITE_API_BACK_URL +
        "&response_type=code&state=" +
        "4ecac45048f746c485f8";
    }
  };
  return {
    zhuYunFun,
  };
}
